<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8" name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"/>
    <title>笔记编辑</title>
    <link rel="stylesheet" href="/public/editor.md/css/editormd.min.css" />
    <link rel="stylesheet" href="/public/css/note-program/edit-note.css">
    <link href="https://cdn.bootcss.com/font-awesome/5.13.0/css/all.css" rel="stylesheet">
    <link rel="stylesheet" href="/public/css/note-program/main.css">
    <link rel="stylesheet" href="/public/css/note-program/reset.css">
    <link rel="stylesheet" href="/public/css/note-program/header.css">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="/public/editor.md/editormd.min.js"></script>
    <script src="/public/editor.md/languages/en.js"></script> 
    <script src="/public/js/note-program/book_struct.js"></script>
    <script src="/public/js/note-program/main.js"></script>
    <script type="text/javascript">
    let clk=0;
    let timer=setInterval(function(){
        let x=document.getElementsByClassName('editormd-preview-close-btn');
        x[0].style.display='none';
        clk++;
        if(clk==5) clearInterval(timer);
    },10);
    function analysis(url) {
        var u2 = url.split("?") //根据？切割
        var u3 = u2[1]         //u2的第二项
        var u4 = u3.split("&") //根据&切割。
        var obj = {}
        for (var i = 0; i < u4.length; i++) {
            var key = u4[i].split("=")[0];
            var value = u4[i].split("=")[1];
            obj[key] = value;
        }
        return (obj);
    };
    $(function() {
        var testmd=editormd("test-editormd", {
            width : "98%",
            height : 615,
            syncScrolling : "single",
            //你的lib目录的路径，和你引入js,css路径一样
            watch: true,
            path    : "/public/editor.md/lib/",
            //这个配置在simple.html中并没有，但是为了能够提交表单，使用这个配置可以让构造出来的HTML代码直接在第二个隐藏的textarea域中，方便post提交表单。
            saveHTMLToTextarea : true,
            htmlDecode      : "style,script,iframe",
            imageUpload : true,
            imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL : "/upload/image",
            emoji           : true,
            taskList        : true,
            tex             : true,  // 默认不解析
            flowChart       : true,  // 默认不解析
            sequenceDiagram : true,  // 默认不解析
            toolbarIcons : function() {
                return ["undo", "redo", "|", "bold", "del","italic","quote","ucwords","uppercase","lowercase", "|", "h1","h2","h3","h4","h5","h6","|","list-ul","list-ol","hr","|","link","reference-link","code","preformatted-text","code-block","table","datetime","html-entities","pagebreak","|","goto-line","watch","preview", "fullscreen","clear","search","|","saveIcon"]
            },
            toolbarIconTexts : {
                saveIcon : "保存并退出"
            },
            toolbarHandlers : {
                saveIcon : function() {
                    let date=new Date();
                    date=dateFormat("YYYY-mm-dd HH:MM", date);
                    let text=this.getValue();
                    let book_title='';
                    request('GET','/modify-note'+window.location.href,null,(res)=>{//判断是新建还是修改
                        let json=JSON.parse(res);
                        if(json.note_title!==undefined){
                            let note=new Note(json.note_title,date,text);
                            note['book_title']=json.book_title;
                            request('POST','/modify-note',note,function(){});
                            setTimeout(()=>{window.location.href='/view/note-program/index.html';}, 500);
                        }
                        else{
                            book_title=json.book_title;
                            let title=prompt("请键入笔记标题进行保存","无标题笔记");
                            let url=`/find-note?book_title=${book_title}&&note_title=${title}`;
                            request('GET',url,null,(res)=>{ //判断是否重名
                                if(res!==''){
                                    alert('同一笔记本下不能创建同名笔记哦~');
                                    return;
                                }
                                else{
                                    let note=new Note(title,date,text)
                                    note['url']=window.location.href;
                                    request('POST','/single-note',note,function(){});
                                    setTimeout(()=>{window.location.href='/view/note-program/index.html';}, 500);
                                }
                            })
                        }
                    })
                }
            },
            onload:function(){
                if(window.screen.width<=851){
                    console.log(12)
                    let pv=document.getElementsByName('preview')[0];
                    console.log(pv)
                    this.unwatch();
                    this.preview();
                    pv[0].click();
                    var pass=document.getElementById("button");
                var event=new MouseEvent("click",{
                    view:pv.ownerDocument.defaultView,
                    bubbles: true,
                    cancelable:true
                });
                pass.dispatchEvent(event);
                    
                }
            }
        });
         
    });
</script>
</head>
<body>
    <!--外部容器-->
<div class="top-bar-wrapper">
    <!-- 中间主体部分容器 -->
    <div class="top-bar">

        <!-- 网站的logo -->
        <h1 class="logo">
            <!-- 此处建议写上网站首页的地址 -->
            <a href="/">
                <img src="../../public/resources/logo.png">
                <span>成为更好的自己</span>
            </a>
        </h1>


        <!-- 菜单栏 -->
        <div class="left-menu">
            <!-- 创建菜单图标 -->
            <ul class="menu-icon">
                <li></li>
                <li></li>
                <li></li>
            </ul>


            <!-- 创建菜单 -->
            <ul class="nav">
                <!-- 这里都要写在超链接里面 -->
                <li><a href="./../myPlan/myTodo.html">个人计划</a></li>
                <li><a href="./../note-program/index.html">我的笔记</a></li>
                <li><a href="./../minder/mind.html">思维导图</a></li>
                <li><a href="./../dataStatistics/dataStatistics.html">数据统计</a></li>
                <li><a href="./../square/square.html">广场</a></li>
            </ul>

        </div>



        <!-- 用户信息 -->
        <div class="user-info">
            <!-- 这里是可以跳转到用户信息详细设置页的，写用户信息详细设置页的地址 -->
            <a href="javascript:void(0);" id="turnUserInfo">
                <img src="../../public/resources/user-pic.jpg" alt="用户头像">
            </a>
        </div>
    </div>
</div>
<div id="layout">
    <div id="test-editormd">
        <textarea style="display:none;"></textarea>
    </div>
</div>
</body>
</html>